/*
 * @Descripttion:
 * @Author: chendingfengmail@163.com
 * @version:
 * @Date: 2021-04-07 08:21:18
 * @LastEditors: Administrator 742573123@qq.com
 * @LastEditTime: 2023-11-11 18:55:28
 */
import Vue from 'vue'
import VueRouter from 'vue-router'

// 进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import store from '@/store/index'
import util from '@/libs/util.js'

// 路由数据
import routes from './routes'

// fix vue-router NavigationDuplicated
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return VueRouterPush.call(this, location).catch(err => err)
}
const VueRouterReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(location) {
  return VueRouterReplace.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

// 导出路由 在 main.js 里使用
const router = new VueRouter({
    // mode: 'history',
    routes
})

/**
 * 路由拦截
 * 权限验证
 */
router.beforeEach(async (to, from, next) => {
    // 确认已经加载多标签页数据
    await store.dispatch('ddAdmin/page/isLoaded')
    // 确认已经加载组件尺寸设置
    await store.dispatch('ddAdmin/size/isLoaded')
    // 进度条
    NProgress.start()
    next();
})

router.afterEach(to => {
  // 进度条
  NProgress.done()
  // 多页控制 打开新的页面
  store.dispatch('ddAdmin/page/open', to)
  // 更改标题
  util.title(to.meta.title)
})

export default router
